// pages/vedio/vedio.js
import request from "../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoGroupList:[],
    navId:null,
    videoList:[],
    videoId:"",
    videoUpdateTime:[],
    isTriggered:false,//标识下拉刷新是否被触发
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getVideoGroupList()
  },
  async getVideoGroupList(){
    let videoGroupListData = await request("/video/group/list")
    this.setData({
      videoGroupList:videoGroupListData.data.slice(0, 14),
      navId:videoGroupListData.data[0].id
    })
    this.getVideoList(this.data.navId)
  },

  changeNav(event){
      wx.showLoading({title:"正在加载"})
    // event.currentTarget.dataset.id
    this.setData({
        navId: event.currentTarget.dataset.id * 1,
        videoList:[]
    })
    this.getVideoList(this.data.navId)
  },

 async getVideoList(navId){
    let videoListData = await request("/video/group",{id: navId})
    this.setData({
      videoList: videoListData.datas.slice(0, 5).map(e=> {
          e.id = e.data.vid
          return e
      })
    })
     wx.hideLoading()
  },

handlePlay(event){
  // 上一个播放的视频，关闭
 let vid = event.currentTarget.id
 this.vid !== vid && this.videoContext && this.videoContext.stop()
 this.vid = vid
 this.setData({
    videoId:vid * 1
 })
 this.videoContext = wx.createVideoContext(vid)
    // 是否有播放记录，如果有跳转到指定位置
 let {videoUpdateTime} =  this.data
 let videoItem = videoUpdateTime.find(item => item.vid === vid*1)
 if(videoItem){
    this.videoContext.seek(videoItem.currentTime)
 }
 this.videoContext.play()
},

handleTimeUpdate(event){
     let videoTimeObj =
         {vid: event.currentTarget.id * 1,
             currentTime: event.detail.currentTime}
    let {videoUpdateTime} =  this.data

    let videoItem = videoUpdateTime.find(item => item.vid === videoTimeObj.vid)
    if(videoItem){
        videoItem.currentTime = event.detail.currentTime
    }else{
        videoUpdateTime.push(videoTimeObj)
    }
    this.setData({
        videoUpdateTime
    })
},
    //视频播放结束调用
handldEnded(event){
    let {videoUpdateTime} =  this.data
    let index = videoUpdateTime.findIndex(item => item.id === event.currentTarget.id)
    videoUpdateTime.splice(index,1)
    this.setData({
        videoUpdateTime
    })
},
handleRefresher(){
    this.setData({
        isTriggered:true
    })
    this.getVideoList(this.data.navId)
    this.setData({
        isTriggered:false
    })
},
//滑动到底部触发
handleScrollToLower(){
  console.log("下一页")
// 数据分页，后端分页，前端分页
},
    toSearch(){
      wx.navigateTo({
          url:"/pages/search/search"
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log("页面下拉刷新")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
      console.log("页面上拉触底")
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage({from}) {
    console.log(from)
      if("button"===from){
          return {
              "title":"button自定义转发标题",
              "page": "/pages/video/video",
              "imageUrl": '/static/images/nvsheng.jpg'
          }
      }else{
          return {
              "title":"menu自定义转发标题",
              "page": "/pages/video/video",
              "imageUrl": '/static/images/nvsheng.jpg'
          }
      }

  }
})